package primary.String;

import java.util.Stack;

/**
 * @Author Elephas
 * @Date 2022/1/9
 **/
public class LongestCommonPrefix {
    public static void main(String[] args) {
        final String[] TEST01 = new String[]{"flower","flow","flight"};
        longestCommonPrefix(TEST01);
        return;
    }
    /*
        解法一，一次检索首位，次首位，……,直到不匹配
     */
    public static String longestCommonPrefix(String[] strs){
        if(strs.length == 0)
            return null;
        String result = "";
        boolean flag = true;
        int index = 0;
        while(flag){
            if(strs[0].length() == index){
                // 数组越界
                flag = false;
                break;
            }
            char first = strs[0].charAt(index);
            for (int i = 1; i < strs.length; i++) {
                if(strs[i].length() == index){
                    // 数组越界
                    flag = false;
                    break;
                }
                char cmp = strs[i].charAt(index);
                if(cmp != first){
                    flag = false;
                    break;
                }
            }
            index++;
            if(flag){
                result += first;
            }
        }
        return result;
    }
    /*
        解法二：横向对比

     */
    public String longestCommonPrefix2(String[] strs){
        String prefix = "";
        return null;
    }
}
